//
//  ViewController.m
//  分享demo
//
//  Created by pc-chtypd056 on 2018/8/3.
//  Copyright © 2018年 CTL. All rights reserved.
//

#import "ViewController.h"
#import "SharedView.h"
#import "SharedViewController.h"
#import "PublicViewController.h"
#import "AZMetaBallCanvas.h"
#import "PopComponent.h"
#import "AIDCTools.h"
#import <AVFoundation/AVFoundation.h>

@interface ViewController ()<UITableViewDelegate,UITableViewDataSource,AVSpeechSynthesizerDelegate>
{
    AVSpeechSynthesizer *_synthesizer;
    AVSpeechUtterance *_utterance;
    BOOL _isSpeak;
}

/** 数据 */
@property (nonatomic, strong) NSArray *dataArray;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    _isSpeak = YES;
    
    _synthesizer = [[AVSpeechSynthesizer alloc]init];
    _synthesizer.delegate = self;
    
    self.dataArray = @[
                       @"分享测试",
                       @"删除数据",
                       @"刻度尺",
                       @"抖音点赞功能",
                       @"贝塞尔曲线",
                       @"QQ一键下班功能",
                       @"FaceID OR TouchID",
                       @"获取健康数据",
                       @"扫一扫",
                       @"截图分或反馈",
                       @"复制与粘贴",
                       @"测试"
                       ];
    
    UITableView *tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0,ScreenWIDTH , ScreenHEIGH) style:UITableViewStyleGrouped];
    tableView.delegate = self;
    tableView.dataSource = self;
    tableView.rowHeight = 50;
    tableView.tableHeaderView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 0, 0.1)];
    
    [self.view addSubview:tableView];
    

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - uitableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
        cell.textLabel.backgroundColor = [UIColor redColor];
    }
    if (indexPath.row < self.dataArray.count) {
        cell.textLabel.text = self.dataArray[indexPath.row];
        
    }
    
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.row == 0) {
//        SharedViewController *sharedVC = [SharedViewController sharedViewController:^(NSString *sharedStr) {
//            NSLog(@"sharedStr：%@",sharedStr);
//        }];
//        [self presentViewController:sharedVC animated:YES completion:nil];
       
        PopComponent *com = [PopComponent popComponentWihtTitle:@"提醒" message:@"开始方考试看看设计费卡士大夫积分卡九分裤扣水电费看守所开发开始的积分会更好师傅就看就看看看就开始哭角度思考开卷考试九分裤角度看看就看扣水电费看课件" style:CTLPopComponentStyleActionSheet];
        CTLAction *cancelActions = [CTLAction actionWithTitle:@"取消" style:CTLActionStyleCancel handlerComplete:^(CTLAction *action) {
            CTLog(@"action.title:%@",action.title);
        }];
        CTLAction *cancelActions1 = [CTLAction actionWithTitle:@"快睡觉1" style:CTLActionStyleDefault handlerComplete:^(CTLAction *action) {
            CTLog(@"action.title:%@",action.title);
        }];
        CTLAction *cancelActions3 = [CTLAction actionWithTitle:@"快睡觉1" style:CTLActionStyleDefault handlerComplete:^(CTLAction *action) {
            CTLog(@"action.title:%@",action.title);
        }];
        CTLAction *cancelActions4 = [CTLAction actionWithTitle:@"快睡觉1" style:CTLActionStyleDefault handlerComplete:^(CTLAction *action) {
            CTLog(@"action.title:%@",action.title);
        }];
        CTLAction *cancelActions2 = [CTLAction actionWithTitle:@"快睡觉2" style:CTLActionStyleDefault handlerComplete:^(CTLAction *action) {
            CTLog(@"action.title:%@",action.title);
        }];
        
        CTLAction *cancel = [CTLAction actionWithTitle:@"确认" style:CTLActionStyleDefault handlerComplete:^(CTLAction *action) {
            if ([action.title isEqualToString:@"确认"]) {
                CTLog(@"action.title:%@",action.title);
            }
        }];
        
        [com addAction:cancelActions];
        [com addAction:cancelActions1];
        [com addAction:cancel];
        [com addAction:cancelActions2];
        [com addAction:cancelActions3];
        [com addAction:cancelActions4];
        [com showWithCompletion:nil];
        
        /**
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"水电费" message:@"是否是东方闪电胜多负少的" preferredStyle:UIAlertControllerStyleActionSheet];
        UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"sdfsd" style:UIAlertActionStyleCancel handler:nil];
        UIAlertAction *cancel4 = [UIAlertAction actionWithTitle:@"大幅度4" style:UIAlertActionStyleDefault handler:nil];
        UIAlertAction *cancel2 = [UIAlertAction actionWithTitle:@"大幅度4" style:UIAlertActionStyleDefault handler:nil];
        UIAlertAction *cancel3 = [UIAlertAction actionWithTitle:@"大幅度4" style:UIAlertActionStyleDefault handler:nil];
        [alertController addAction:cancel4];
        [alertController addAction:cancel2];
        [alertController addAction:cancel3];
        [alertController addAction:cancel];
        [self presentViewController:alertController animated:YES completion:nil];*/
        
    }else if (indexPath.row == 11){
        [[AIDCTools shared]showAIDCWithStatus:^(AIDCStatus status, NSError *error) {
            
        }];
    }else{
        PublicViewController *vc = [[PublicViewController alloc]init];
        vc.indexPath = indexPath;
        vc.titleStr = self.dataArray[indexPath.row];
        [self.navigationController pushViewController:vc animated:YES];
    }
    
    _utterance = [[AVSpeechUtterance alloc]initWithString:self.dataArray[indexPath.row]];
    _utterance.rate = 0.5;
    _utterance.pitchMultiplier = 1;
    _utterance.volume = 1;
    _utterance.preUtteranceDelay = 1;
    _utterance.postUtteranceDelay = 1;
    
    AVSpeechSynthesisVoice *voice = [AVSpeechSynthesisVoice voiceWithLanguage:@"zh-CN"];
    _utterance.voice = voice;
    [_synthesizer speakUtterance:_utterance];
    
    if (_isSpeak) {
        [_synthesizer pauseSpeakingAtBoundary:AVSpeechBoundaryImmediate];//continueSpeaking
    }else{
        [_synthesizer continueSpeaking];
    }
}

#pragma mark - AVSpeechSynthesizerDelegate
//已经开始
- (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didStartSpeechUtterance:(AVSpeechUtterance *)utterance{
    
    _isSpeak = YES;
}
//已经说完
- (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didFinishSpeechUtterance:(AVSpeechUtterance *)utterance{
    _isSpeak = NO;
    //如果朗读要循环朗读，可以在这里再次调用朗读方法
    //[_avSpeaker speakUtterance:utterance];
}
//已经暂停
- (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didPauseSpeechUtterance:(AVSpeechUtterance *)utterance{
    _isSpeak = NO;
}
//已经继续说话
- (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didContinueSpeechUtterance:(AVSpeechUtterance *)utterance{
    _isSpeak = YES;
}
//已经取消说话
- (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didCancelSpeechUtterance:(AVSpeechUtterance *)utterance{
    _isSpeak = NO;
}
//将要说某段话
- (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer willSpeakRangeOfSpeechString:(NSRange)characterRange utterance:(AVSpeechUtterance *)utterance{
    
}

@end
